<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/7/30
 * Time: 14:53
 */

namespace app\commi\controller;

use think\Db;
use think\Request;

class Rowingls extends Base
{
    private $boat_type = [
        '1' => '1x',
        '2' => '2-',
        '3' => '2+',
        '4' => '4-',
        '5' => '4+',
        '6' => '4x',
        '7' => '2x',
        '8' => '8+'
    ];
    /**
     * 赛艇临时成绩
     * @return mixed|\think\response\Json
     */
    public function index(){
        $match = Db::table('up_match')
            ->where('match_committee_id', session('com_id'))
            ->field('match_id')
            ->find();
        $group = Db::table('up_match_apply_group')
            ->join('up_match_group', 'mg_id = mag_group_id', 'left')
            ->where('mag_match_id', $match['match_id'])
            ->field('mg_id, mg_name')
            ->select();
        if(request()->isAjax ()){
            $group_id = input('group_id'); //  组别
            $lc_id = input('lc_id'); // 轮次
            $zc_id = input('zc_id'); // 组次
            $boat_type = input('boat_type'); // 船型
            $distance = input('distance'); // 赛道距离
            
            $where['umtr.match_id'] = $match['match_id'];
            $where['umtr.type'] = 2;
            // 组别
            if(isset($group_id) && $group_id != ''){
                $where['umtr.ma_group'] = $group_id;
            }
            /*else{
                $where['umtr.ma_group'] = $group[0]['mg_id'];
            }*/
            // 轮次
            if(isset($lc_id) && $lc_id != ''){
                $where['umtr.mt_id'] = $lc_id;
            }
            /*else{
                $where['umtr.mt_id'] = $lunci[0];
            }*/
            // 组次
            if(isset($zc_id) && $zc_id != ''){
                $where['umtr.mr_id'] = $zc_id;
            }
            /*else{
                $where['umtr.mr_id'] = $lunci[0];
            }*/
            // 船型
            if(isset($boat_type) && $boat_type != ''){
                $where['umtr.ma_boat_type'] = $boat_type;
            }
            /*else{
                $where['umtr.ma_boat_type'] = 1;
            }*/
            // 赛道距离
            if(isset($distance) && $distance != ''){
                $where['umtr.ma_distance'] = $distance;
            }
            /*else{
                $where['umtr.ma_distance'] = 500;
            }*/
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up__match_yacht_record')
                ->alias('umyr')
                ->join('up__match_turns_rounds umtr', 'umtr.id = umyr.match_turns_rounds_id', 'left')
                ->join('up_u_user uuu', 'uuu.user_id = umyr.user_id', 'left')
                ->join('up_match_group umg', 'umg.mg_id = umtr.ma_group', 'left')
                ->join('up__match_rounds umr', 'umr.mr_id = umtr.mr_id', 'left')
                ->join('up__match_turns umt', 'umt.mt_id = umtr.mt_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $count2 = Db::table('up__match_yacht_record')
                ->alias('umyr')
                ->join('up__match_turns_rounds umtr', 'umtr.id = umyr.match_turns_rounds_id', 'left')
                ->join('up_u_user uuu', 'uuu.user_id = umyr.user_id', 'left')
                ->join('up_match_group umg', 'umg.mg_id = umtr.ma_group', 'left')
                ->join('up__match_rounds umr', 'umr.mr_id = umtr.mr_id', 'left')
                ->join('up__match_turns umt', 'umt.mt_id = umtr.mt_id', 'left')
                ->where($where)
                ->column('umyr.id');
            $lists = Db::table('up__match_yacht_record')
                ->alias('umyr')
                ->join('up__match_turns_rounds umtr', 'umtr.id = umyr.match_turns_rounds_id', 'left')
                ->join('up_u_user uuu', 'uuu.user_id = umyr.user_id', 'left')
                ->join('up_match_group umg', 'umg.mg_id = umtr.ma_group', 'left')
                ->join('up__match_rounds umr', 'umr.mr_id = umtr.mr_id', 'left')
                ->join('up__match_turns umt', 'umt.mt_id = umtr.mt_id', 'left')
                ->field('umyr.id, umg.mg_name, umtr.ma_distance, umtr.ma_boat_type, umt.mt_name, 
                umr.mr_name, umyr.track_num, umyr.ls_status, umyr.user_id, umtr.match_id, umyr.temp_score, 
                umyr.accumulated_time')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('umtr.mr_id, umyr.temp_score asc')
                ->select();
            foreach($lists as $k => $v){
                // 名次
                $lists[$k]['mc'] = $k + 1;
                // 报名信息
                $apply = Db::table('up_match_apply_info')
                    ->where('ma_user_id', $v['user_id'])
                    ->where('ma_match_id', $v['match_id'])
                    ->field('ma_team_name')
                    ->find();
                $lists[$k]['ma_team_name'] = $apply['ma_team_name']; // 队名
                $lists[$k]['member_type'] = session('cm_member_type');
                
                $join_user = Db::table('up_match_apply_info')
                    ->where('ma_team_name', $apply['ma_team_name'])
                    ->where('ma_match_id', $v['match_id'])
                    ->column('ma_user_name');
                $lists[$k]['ma_user_name'] = implode(',', $join_user);
            }
            $str = '';
            if($count2){
                $str = implode($count2,',');
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'count2' => $str, 'data' => $lists]);
        }
    
        $lunci = Db::table('up__match_turns')->field('mt_id, mt_name')->select();
        $zuci = Db::table('up__match_rounds')->field('mr_id, mr_name')->select();
        $this->assign([
            'group' => $group,
            'lunci' => $lunci,
            'zuci' => $zuci
        ]);
        return $this->fetch();
    }
    
    /**
     * 提交异议
     * @return mixed|\think\response\Json
     */
    public function commitYiyi(){
        $id = input('id');
        if(Request::instance()->isPost()){
            $ls_yiyi_content = input('ls_yiyi_content');
            $update = [
                'ls_yiyi_content' => $ls_yiyi_content,
                'ls_status' => 2,
                'ls_commit_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up__match_yacht_record')->where('id', $id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '提交成功']);
            }else{
                return json(['code' => 100, 'msg' => '提交失败']);
            }
        }
        $this->assign([
            'id' => $id
        ]);
        $lose = $this->lostTime();
        if($lose == 1){
            return $this->fetch('/lose');
        }else{
            return $this->fetch('rowingls/commityiyi');
        }
    }
    
    /**
     * 导出
     * @return \think\response\Json
     */
    public function outExcel(){
        set_time_limit(0);
        $ids = input('ids');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '没有需要导出的数据']);
        }
        $lists = Db::table('up__match_yacht_record')
            ->alias('umyr')
            ->join('up__match_turns_rounds umtr', 'umtr.id = umyr.match_turns_rounds_id', 'left')
            ->join('up_u_user uuu', 'uuu.user_id = umyr.user_id', 'left')
            ->join('up_match_group umg', 'umg.mg_id = umtr.ma_group', 'left')
            ->join('up__match_rounds umr', 'umr.mr_id = umtr.mr_id', 'left')
            ->join('up__match_turns umt', 'umt.mt_id = umtr.mt_id', 'left')
            ->field('umyr.id, umg.mg_name, umtr.ma_distance, umtr.ma_boat_type, umt.mt_name, 
                umr.mr_name, umyr.track_num, umyr.ls_status, umyr.user_id, umtr.match_id, umyr.temp_score, 
                umyr.accumulated_time')
            ->where('umyr.id', 'IN', $ids)
            ->order('umyr.temp_score asc')
            ->select();
        foreach($lists as $k => $v){
            // 名次
            $lists[$k]['mc'] = $k + 1;
            // 报名信息
            $apply = Db::table('up_match_apply_info')
                ->where('ma_user_id', $v['user_id'])
                ->where('ma_match_id', $v['match_id'])
                ->field('ma_team_name')
                ->find();
            $lists[$k]['ma_team_name'] = $apply['ma_team_name']; // 队名
    
            $join_user = Db::table('up_match_apply_info')
                ->where('ma_team_name', $apply['ma_team_name'])
                ->where('ma_match_id', $v['match_id'])
                ->column('ma_user_name');
            $lists[$k]['ma_user_name'] = implode(',', $join_user);
            $lists[$k]['boat_type'] = $this->boat_type[$v['ma_boat_type']];
        }
        $cellname = [
            ['mg_name', '组别', 20, 'LEFT'],
            ['ma_distance', '赛道距离', 13, 'LEFT'],
            ['boat_type', '船型', 10, 'LEFT'],
            ['mt_name', '轮次', 10, 'LEFT'],
            ['mr_name', '组次', 15, 'LEFT'],
            ['track_num', '道次', 10, 'LEFT'],
            ['temp_score', '成绩', 10, 'LEFT'],
            ['mc', '名次', 10, 'LEFT'],
            ['ma_team_name', '队名', 20, 'LEFT'],
            ['accumulated_time', '总滑行时间', 20, 'LEFT'],
            ['ma_user_name', '参赛人员', 30, 'LEFT'],
        ];
        $res = exportExcel('临时成绩表', 'lsMark', $cellname, $lists);
        return json($res);
    }
}